package com.lg.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.cms.entity.*;
import com.lg.cms.mapper.*;
import com.lg.cms.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lg.cms.utils.RedisUtil;
import com.lg.cms.vo.LayerTableVO;
import com.lg.cms.vo.news.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户-文章多对多 评论表 服务实现类
 * </p>
 *
 * @author lg
 * @since 2020-09-21
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    ArticleTypeMapper articleTypeMapper;

    @Autowired
    SuperUserMapper superUserMapper;
    @Autowired
    UserMapper userMapper;

    @Autowired
    CommentMapper commentMapper;

    @Resource
    RedisUtil redisUtil;





    /**
     * 按照 审核状态 分页查询评论实现类
     * @param page
     * @param check
     * @return
     */
    @Override
    public LayerTableVO<Comment> selectCommentByCheck(Page page, Boolean check) {
        QueryWrapper<Comment> queryWrapper=new QueryWrapper<>();

        if (check==null){
            queryWrapper.isNull("`check`");
        }else{
            queryWrapper.eq("`check`",check);
        }
        IPage<Comment> iPage= commentMapper.selectPage(page, queryWrapper);

        for ( Comment c:iPage.getRecords() ) {
            c.setArticle(articleMapper.selectById(c.getArticleId()).getTitle());
            c.setUser(userMapper.selectById(c.getUserId()).getNickname());
        }

        LayerTableVO<Comment> layerData=new LayerTableVO<>();

        if (iPage.getTotal()==0) layerData.setCode(1);
        else layerData.setCode(0);

        layerData.setMsg("没有数据");
        layerData.setCount(iPage.getTotal());
        layerData.setData(iPage.getRecords());

        return layerData;

    }

    /**
     * 按照查找文章个人根据id实现类
     * @param authorId
     * @return
     */
    @Override
    public Article selectAuthorIdCheck( Integer authorId ) {

        Article article=articleMapper.selectById(authorId);

        return article;

    }

    /**
     * 设置文章的 审核状实现类
     * @param id
     * @param check
     * @return
     */
    @Override
    public int updateCheck(Integer id, Boolean check) {
        Comment comment=new Comment();
        comment.setId(id);
        comment.setCheck(check);

        boolean tf=this.saveOrUpdate(comment);
        if (tf){
            return 1;
        }
        return 0;

    }

    /**
     * 批量设置文章的 审核状实现类
     * @param ids
     * @param check
     * @return
     */
    @Override
    public boolean updateChecks(String ids, Boolean check) {
        String[] stids = ids.split(",");
        List<Integer> listIds = new ArrayList<>();
        for(String str:stids){
            listIds.add((int)Integer.parseInt(str));
        }
        List<Comment> comments=new ArrayList<>();
        for (Integer id: listIds
        ) {
            Comment comment=new Comment();
            comment.setId(id);
            comment.setCheck(check);
            comments.add(comment);
        }

        return this.saveOrUpdateBatch(comments);
    }

    /**
     * 删除文章里的评论实现类
     * @param id
     * @return
     */
    @Override
    public int deletCheckById(Integer id) {
        int n=commentMapper.deleteById(id);

        return n;
    }

    /**
     *
     * 批量删除文章里的评论实现类
     * @param ids
     * @return
     */
    @Override
    public int deletCheckByIds(String ids) {
        String[] stids = ids.split(",");
        List<Long> listIds = new ArrayList<>();
        for(String str:stids){
            listIds.add((long)Integer.parseInt(str));
        }

        return  commentMapper.deleteBatchIds(listIds);
    }


    /**
     * 获取某文章评论vo列表(第一页)
     * @param articleId
     * @return CommentVOList
     */
    @Override
    public List<CommentVO> getListByArticleId(Integer articleId) {
        Page<Comment> page = new Page<>(1, (Integer)redisUtil.get("cms:pageSize"));
        List<CommentVO> list = new ArrayList<>();
        commentMapper.selectPage(page, this.getWrapper(articleId));
        page.getRecords().forEach(v -> list.add(this.toCommentVO(v)));
        return list;
    }

    /**
     * 获取某文章评论page对象
     * @param articleId
     * @return Page
     */
    @Override
    public Page<CommentVO> getPageByArticleId(Integer articleId, Integer pageNum) {
        Page page = new Page<>(pageNum, (Integer)redisUtil.get("cms:pageSize"));
        List<CommentVO> list = new ArrayList<>();
        commentMapper.selectPage(page, this.getWrapper(articleId));
        page.getRecords().forEach(v -> list.add(this.toCommentVO((Comment) v)));
        page.setRecords(list);
        return page;
    }

    @Override
    public CommentVO toCommentVO(Comment comment) {
        CommentVO vo = new CommentVO();
        User user = userMapper.selectById(comment.getUserId());
        if(user == null){
            return null;
        }
        vo.setUserNickname(user.getNickname());
        vo.setContent(comment.getContent());
        vo.setCreateTime(comment.getCreateTime());

        vo.setComment(comment);
        return vo;
    }

    private QueryWrapper<Comment> getWrapper(Integer articleId){

        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        if(articleId != null)
            wrapper.eq("article_id", articleId);
        wrapper.orderByDesc("create_time");
        wrapper.eq("`check`", true);
        return wrapper;
    }

}
